{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import TreeMap\n",
    "import pandas as pd\n",
    "\n",
    "column_names = [\"name\", \"value\", \"month_change\"]\n",
    "df = pd.read_csv(\"bmpi-dev-protfolio.csv\", names=column_names)\n",
    "\n",
    "data = []\n",
    "colors = []\n",
    "\n",
    "df = df.sort_values('value', ascending=False)\n",
    "\n",
    "for index, row in df.iterrows():\n",
    "    data.append({\"value\": row[\"value\"], \"name\": row[\"name\"] + \"\\n\\n\" + str(row[\"month_change\"])})\n",
    "    if row[\"month_change\"] < 0:\n",
    "        colors.append('rgba(15, 174, 157, 1)')\n",
    "    else:\n",
    "        colors.append('rgba(254, 91, 75, 1)')\n",
    "            \n",
    "c = (\n",
    "    TreeMap(init_opts=opts.InitOpts(width=\"1650px\", height=\"700px\"))\n",
    "    .add(\n",
    "        series_name=\"组合21年7月份持仓涨跌\", \n",
    "        data=data,\n",
    "        width=1500,\n",
    "        height=600,\n",
    "        color_mapping_by='index',\n",
    "        levels=[\n",
    "            opts.TreeMapLevelsOpts(\n",
    "                treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(\n",
    "                    border_color=\"#FFFFFF\", border_width=1, gap_width=1,\n",
    "                    ),\n",
    "            )\n",
    "        ],\n",
    "        label_opts=opts.LabelOpts(color='rgba(255, 255, 255, 1)', position='inside', font_size=12, font_weight='bolder')\n",
    "    ).set_colors(colors=colors)\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"被动收入投资组合\"))\n",
    "    .render(\"bmpi-dev-protfolio/treemap-202107.html\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
